(function(){
    function GetMachineMonitor(){
        this.init();
  
     
       
    }

    GetMachineMonitor.prototype = {
        constructor:GetMachineMonitor,
        init:function(){
            this.nodata = '<tr><td  colspan="8">暂无信息</td></tr>';
            var _this = this;    
          
            
            

                this.getMonitorList(1);
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getMonitorList(1);
                });
                $.extend({page:function(pn){
                    _this.getMonitorList(pn);
                }});

                this.addNewMachineInfo();
                this.setDaxiaoPiaoInfo();
                $("#mcanuse").click(function(){
                    $("#munuse").prop("checked", false);
                    _this.getMonitorList(1);
                
                });

                $("#munuse").click(function(){
                    $("#mcanuse").prop("checked", false);
                    _this.getMonitorList(1);
                    
                });

            

        },
        getMonitorList:function( pn ){
          
            var _this = this;
            var machineId = $("#machineNum").val();
            var isUse = $(".sel-isuse:checked").val();
            isUse = isUse?"&state="+isUse:"";
            if( machineId )
            {
                machineId = "&siteid="+machineId;
            }
            var pageNo = pn;
          
            $.ajax({
                type:"get",
                url:"/sys?fid=301"+machineId+isUse+"&ps=20&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
               
  
                    if( code == 1 )
                    {
                       
                        var data = info.data;
                        var count = info.count;
                        // state  -1撤单、0.未分配、1.已分配、2.已接单
                        var stateArr = ["不可用","可用"];
                        var jiedanStatus = ["未接单","接单"];
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var siteid  = value.siteid ;
    
                                var password = value.password;
                                var time = value.time;
                                var cdesc = value.cdesc;
                                var stateIndex = value.state;
                                var state = stateArr[stateIndex];
                                var wmoney = value.wmoney;//机器余额
                                var status = value.status;//1 接单 0 未接单
                                var rebate = value.rebate;//返点 竞彩
                                var bdrebate = value.bdrebate || "";//返点 北单
                                var settle = value.settle;//结算日
                                var jiedanDesc = jiedanStatus[status]; 
                                var statusClass = status == 0 ? 'machinestatus' : "busy";
                               
                               
                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                 <td  class="machineisXiaipiao" ><input type="checkbox" class="flat setflat  flatisxiaopiao"  id="checkbox'+siteid+'"   value="'+siteid+'"  ></td>\
                                                <td >'+siteid+'<span  class="isdaxiaopiao" id="daxiaopiao'+siteid+'"></span></td>\
                                                <td>'+wmoney+'</td>\
                                                <td>'+password+'</td>\
                                                <td>'+cdesc+'</td>\
                                                <td>'+state+'</td>\
                                                <td  id="machine'+siteid+'" class="'+statusClass+'">'+jiedanDesc+'</td>\
                                                <td>\
                                                <a  href="javascript:;" class="btn btn-info  editorBtn"    data-siteid="'+siteid+'"  data-pwd="'+password+'"  data-cdesc="'+cdesc+'"  data-stateindex="'+stateIndex+'"  data-pageno="'+pageNo+'"  data-rate="'+rebate+'"  data-bdrate="'+bdrebate+'"   data-settime="'+settle+'">编辑</a>\
                                                <a  href="javascript:;" class="btn btn-info  addmoneyBtn"    data-siteid="'+siteid+'"  data-pwd="'+password+'"  data-cdesc="'+cdesc+'"  data-stateindex="'+stateIndex+'"  data-pageno="'+pageNo+'">加款</a>\
                                                <a  href="javascript:;" class="btn btn-info  cutdownBtn"   data-siteid="'+siteid+'"  data-pwd="'+password+'"  data-cdesc="'+cdesc+'"  data-stateindex="'+stateIndex+'"  data-pageno="'+pageNo+'">扣款</a>\
                                                <a  href="balance.html?mid='+siteid+'" class="btn btn-info  showliushuiBtn"  >流水</a>\
                                                 <a  href="javascript:;" class="btn btn-info  setlotBtn"   data-siteid="'+siteid+'"  >机器配置</a>\
                                                </td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                     
                            //  _this.getMachineStatus();
                            //  大小票信息
                         
                            _this.getXiaopiaoList(pageNo);

                            // 修改
                            $(".editorBtn").click(function(){
                                  $("#editorMachineModal").modal();
                                  _this.editorMachineInfo(this);

                            });
                             // 加款
                             $(".addmoneyBtn").click(function(){
                                $("#addmoneyMachineModal").modal();
                                _this.addMoneyMachineInfo(this);

                             });
                              // 扣款
                              $(".cutdownBtn").click(function(){
                                $("#cutdownmoneyMachineModal").modal();
                                _this.cutMoneyMachineInfo(this);

                             });

                            //  机器配置
                            $(".setlotBtn").unbind("click").click(function(){
                                var matchind = $(this).data("siteid");
                                $("#setgameframe").attr("src",'sitegame.html?sitid='+matchind);
                                $("#lottypemid").html(matchind);
                                $("#lottypeMachineModal").modal();
                                
                             });
                         

                            //  开启关闭机器
                            
                          
                            
                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);



                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        // 添加机器
        addNewMachineInfo:function(){
            var _this = this;
            $("#addNewMachine").click(function(){
                $("#addMachineModal").modal();
            });

            $("#addMachineBtn").unbind("click").click(function(){
                 var machineNum = $("#addmachineNum").val();
                 var machinepwd = $("#addmachinepwd").val();
                 var machineDesc = $("#machineDesc").val();
                
                 if(!machineNum )
                 {
                    $("#machineNum").focus();
                    return false;
                 }
                 if(!machinepwd )
                 {
                    $("#machinepwd").focus();
                    return false;
                 }
      
                 $.ajax({
                     type:"get",
                     url:"/sys?fid=302&siteid="+machineNum+"&password="+machinepwd+"&cdesc="+machineDesc,
                     dataType:"json",
                     success:function( info ){
                         var code = info.code;
                         var desc = info.desc;
                         if( code == 1 )
                         {
                             
                             $("#addMachineBtn").popover("show");
                             $("#addmachineNum,#addmachinepwd,#machineDesc").val("");
                             _this.getMonitorList(1);
                             setTimeout(function(){
                                $("#addMachineBtn").popover("hide");
                                $("#addMachineModal").modal("hide");
                            }, 800);
                             

                         }
                         else
                         {
                            $("#editorMachineModal").modal("hide");
                             if( code == 2 )
                             {
                               
                                $("#myModalLabel").html("你还未登录");
                                $("#loginoutModal").modal();
                                setTimeout(function(){
                                    location.href = "login.html";
                                }, 800);
                             }
                             else
                             {
                                $("#myModalLabel").html(desc);
                                $("#loginoutModal").modal();

                             }
                         }


                     }
                 });







            });





        },
        // 机器信息编辑
        editorMachineInfo:function(that){
            var siteid  = $(that).data("siteid");
            // var machine = $(that).data("mid");
            var password = $(that).data("pwd");
            var cdesc = $(that).data("cdesc");
            var stateIndex = $(that).data("stateindex");
            var rate = $(that).data("rate");
            var bdrate = $(that).data("bdrate");
            var settime = $(that).data("settime");
            var pageNo = parseInt( $(that).data("pageno") );
            var _this = this;

            // $("#editorMachineId").val(siteid);
            $("#editorMachineNum").val(siteid);
            $("#editorMachinePwd").val(password);
            $("#editorMachinedesc").val(cdesc);
            $("#editorMachineStatus").val(stateIndex);
            $("#editorMachinerebate").val(rate);
            $("#editorMachinejiesuan").val(settime);
            $("#editorMachinebdrebate").val(bdrate);
        

            $("#modifyMachineBtn").unbind("click").click(function(){
                    var newsiteid  =  $("#editorMachineId").val();
                    var newmachine =  $("#editorMachineNum").val();
                    var newpassword = $("#editorMachinePwd").val();
                    var newcdesc = $("#editorMachinedesc").val();
                    var newstateIndex =  $("#editorMachineStatus").val();
                    var newrate = $("#editorMachinerebate").val() || 0;
                    var newtime = $("#editorMachinejiesuan").val() ;
                    var newbdRate = $("#editorMachinebdrebate").val() || 0;
                
                    if( !newmachine )
                    {
                        $("#editorMachineNum").focus();
                        return false;
                    }
                    if( !newpassword )
                    {
                        $("#editorMachinePwd").focus();
                        return false;
                    }
                    $.ajax({
                        type:"post",
                        url:"/sys?fid=303",
                        data:{
                            state:newstateIndex,
                            siteid:newmachine,
                            password:newpassword,
                            cdesc:newcdesc,
                            rebate:newrate,
                            bdrebate:newbdRate,
                            time:newtime
                        },
                        success:function( info ){
                               var code = info.code;
                                var desc = info.desc;
                                if( code == 1 )
                                {
                                    _this.getMonitorList(pageNo);
                                    $("#modifyMachineBtn").popover("show");
                                    setTimeout(function(){
                                        $("#modifyMachineBtn").popover("hide");
                                        $("#editorMachineModal").modal("hide");
                                    }, 800);
                                    

                                }
                                else
                                {
                                    $("#editorMachineModal").modal("hide");
                                    if( code == 2 )
                                    {
                                    
                                        $("#myModalLabel").html("你还未登录");
                                        $("#loginoutModal").modal();
                                        setTimeout(function(){
                                            location.href = "login.html";
                                        }, 800);
                                    }
                                    else
                                    {
                                        $("#myModalLabel").html(desc);
                                        $("#loginoutModal").modal();

                                    }
                                }
                            

                        }


                    });







            });

    

        },
        // 机器的登录状态
        getMachineStatus:function(){
            $.ajax({
                type:"get",
                url:"/sys?fid=307",
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    if( code == 1 )
                    {
                        var data = info.data;
                        if( data )
                        {
                            data.forEach(function( item ){
                                var siteid = item.siteid;
                                var num = item.num;
                                var statusDesc = ["空闲","接单"];//空闲表示机器仅仅登录了
                                if( $("#machine"+siteid) )
                                {
                                    $("#machine"+siteid).removeClass("machinestatus");
                                    num == 0 ? $("#machine"+siteid).addClass("leisure") : $("#machine"+siteid).addClass("busy");
                                    
                                    $("#machine"+siteid).html( statusDesc[num]);
                                }
                            });

                            $(".machinestatus").html("离线");
                        }

                    }


                }
            });

        },
        // 设置大小票信息
        setDaxiaoPiaoInfo:function(){
            var _this = this;
            $("#saveBtnMoney").unbind("click").click(function(){
                var money = $("#xiaopiaomoney").val();
                if( !money )
                {
                    $("#xiaopiaomoney").focus();
                    return false;
                }
                $.ajax({
                    type:"get",
                    url:"/sys?fid=501&desc=moneyLimit&cdesc="+money,
                    success:function( info ){
                        var code = info.code;
                        if( code == 1 )
                        {
                            $("#xpMoney").html("("+money+")");
                           
                            $("#saveBtnMoney").popover("show");
                            setTimeout(function(){
                               $("#saveBtnMoney").popover("hide");
                           }, 800);

                        }
                        else
                        {
                            if( code == 2 )
                            {
                              
                               $("#myModalLabel").html("你还未登录");
                               $("#loginoutModal").modal();
                               setTimeout(function(){
                                   location.href = "login.html";
                               }, 800);
                            }
                            else
                            {
                               $("#myModalLabel").html(desc);
                               $("#loginoutModal").modal();

                            }

                        }
                    }
                });

            });

            $("#saveBtnTime").unbind("click").click(function(){
                var time = $("#xiaopiaotime").val();
                if( !time )
                {
                    $("#xiaopiaotime").focus();
                    return false;
                }
                $.ajax({
                    type:"get",
                    url:"/sys?fid=501&desc=beforeMinute&cdesc="+time,
                    success:function( info ){
                        var code = info.code;
                        if( code == 1 )
                        {
                            $("#endTime").html("("+time+")");
                            $("#saveBtnTime").popover("show");
                            setTimeout(function(){
                               $("#saveBtnTime").popover("hide");
                           }, 800);

                        }
                        else
                        {
                            if( code == 2 )
                            {
                              
                               $("#myModalLabel").html("你还未登录");
                               $("#loginoutModal").modal();
                               setTimeout(function(){
                                   location.href = "login.html";
                               }, 800);
                            }
                            else
                            {
                               $("#myModalLabel").html(desc);
                               $("#loginoutModal").modal();

                            }

                        }

                    }
                });

            });

        },
        // 小票列表
        getXiaopiaoList:function( pn ){

            var _this=  this;
            $.ajax({
                type:"get",
                url:"/sys?fid=502",
                success:function( info ){
                    var code = info.code;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var xSites = data["xiao.sites"];
                        var moneyLimit = data.moneyLimit;
                        var beforeMinute = data.beforeMinute;
                        $("#xpMoney").html("("+moneyLimit+")");
                        $("#endTime").html("("+beforeMinute+")");
                   

                        
                        if( xSites )//设置大小票信息
                        {
                            xSites = xSites
                            var siteArr = xSites.split(",");
                            siteArr.forEach(function( item ){
                                $("#daxiaopiao"+item).html(" (小票机器)");
                                $("#checkbox"+item).addClass("selectflat");
                            });

                            $(".flatisxiaopiao").not(".selectflat").addClass("selectflatnoxp");
                        }
                        else
                        {
                            $(".flatisxiaopiao").addClass("selectflatnoxp");

                        }

                          //  选择设置小票的机器
                          $("#setXiaopiaoMachine").unbind("click").click(function(){
                            var onOff = 0;
                            $(".selectflatnoxp:checked").each(function(){
                                onOff += 1;
                            });

                            if(onOff)
                            {
                                $("#headTicketsModal").modal();
                            }
                            else
                            {
                                $("#myModalLabel").html("请先选择需要设置的机器");
                                $("#loginoutModal").modal();

                            }
                    });
                    // 选择取消的小票机器
                    $("#cancelXiaopiaoMachine").unbind("click").click(function(){
                        var onOff = 0;
                        $(".selectflat:checked").each(function(){
                            onOff += 1;
                        });
                        if(onOff)
                        {
                            $("#headTicketsModalcancel").modal();
                        }
                        else
                        {
                            $("#myModalLabel").html("请先选择需要取消小票的机器");
                            $("#loginoutModal").modal();

                        }
                   });


                         // 确定设置成小票机器
                         $("#confirmheadBtn").unbind("click").click(function(){
                           
                            _this.setXpMachine( xSites, pn );

                        });

                         // 确定取消小票机器
                         $("#cancelXpBtn").unbind("click").click(function(){
                           
                            _this.cancelXpMachine( xSites, pn );

                        });
                       


                    }
                    



                }
            });



        },

        // 设置小票机器
        setXpMachine:function(xSites, pn){
   
            var selMachineArr = [];
            $(".selectflatnoxp:checked").each(function(){
                var tid = $(this).val();
                selMachineArr.push(tid);
            });

            var sites = xSites ? ","+xSites : "";
     
            var machineStr = selMachineArr.join(",");
            machineStr += sites;
            machineStr = ","+machineStr+",";
       

            $.ajax({
                type:"get",
                url:"/sys?fid=501&desc=xiao.sites&cdesc="+machineStr,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $.page(pn)
                        $("#headTicketsModal").modal("hide");

                        setTimeout(function(){
                            $("#myModalLabel").html("设置小票机器成功");
                            $("#loginoutModal").modal();

                        }, 500);


                    }
                    else
                    {
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }


                }

            });



        },
         // 取消小票机器
         cancelXpMachine:function(xSites, pn){
            var selMachineArr = [];//存放要取消的小票机器 编号
            $(".selectflat:checked").each(function(){
                var tid = $(this).val();
                selMachineArr.push(tid);
            });
            var sendIdArr = [];
            if( xSites )
            {
                var siteArr = xSites.split(",");
                siteArr.forEach(function( item ){
                    if( selMachineArr.indexOf(item) == -1 )
                    {
                        sendIdArr.push( item );
                    }
                });
            }
            else
            {
                sendIdArr = selMachineArr;
            }

           var machineStr = sendIdArr.join(",");
    
            $.ajax({
                type:"get",
                url:"/sys?fid=501&desc=xiao.sites&cdesc="+machineStr,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        $.page(pn)
                        $("#headTicketsModalcancel").modal("hide");

                        setTimeout(function(){
                            $("#myModalLabel").html("取消小票机器成功");
                            $("#loginoutModal").modal();


                        }, 500);
                       

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 1000);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }


                }

            });



        },
         // 机器加款
         addMoneyMachineInfo:function(that){
            var siteid  = $(that).data("siteid");   
            var pageNo = parseInt( $(that).data("pageno") );
            $("#addmoneyMachineNum").val(siteid);
            var _this = this;
            $("#addmoneyMachineBtn").unbind("click").click(function(){
                    var addmoney  =  $("#addtotalmomey").val();
                    var mark = $("#addmoneymark").val();
                    if( !addmoney )
                    {
                        $("#addtotalmomey").focus();
                        return false;
                    }
                    var pData = {
                        siteid:siteid,
                        wmoney:addmoney,
                        ticketid:mark
                    };
                    $.ajax({
                        type:"post",
                        url:"/sys?fid=310&type=3",
                        data:pData,
                        success:function( info ){
                               var code = info.code;
                                var desc = info.desc;
                                if( code == 1 )
                                {
                                    _this.getMonitorList(pageNo);
                                    $("#addmoneyMachineBtn").popover("show");
                                    setTimeout(function(){
                                        $("#addmoneyMachineBtn").popover("hide");
                                        $("#addmoneyMachineModal").modal("hide");
                                    }, 300);
                                    

                                }
                                else
                                {
                                    $("#addmoneyMachineModal").modal("hide");
                                    if( code == 2 )
                                    {
                                    
                                        $("#myModalLabel").html("你还未登录");
                                        $("#loginoutModal").modal();
                                        setTimeout(function(){
                                            location.href = "login.html";
                                        }, 800);
                                    }
                                    else
                                    {
                                        $("#myModalLabel").html(desc);
                                        $("#loginoutModal").modal();

                                    }
                                }
                            

                        }


                    });







            });

    

        },
          // 机器扣款
        cutMoneyMachineInfo:function(that){
            var siteid  = $(that).data("siteid");   
            var pageNo = parseInt( $(that).data("pageno") );
            $("#cutdownmoneyMachineNum").val(siteid);
            var _this = this;
            $("#cutdownmoneyMachineBtn").unbind("click").click(function(){
                    var cutmoney  =  $("#cutdownmoney").val();
                    var mark  =  $("#cutmoneymark").val();
                    if( !cutmoney )
                    {
                        $("#cutdownmoney").focus();
                        return false;
                    }
                    var pData = {
                        siteid:siteid,
                        wmoney:cutmoney,
                        ticketid:mark
                    };
                    $.ajax({
                        type:"post",
                        url:"/sys?fid=310&type=0",
                        data:pData,
                        success:function( info ){
                               var code = info.code;
                                var desc = info.desc;
                                if( code == 1 )
                                {
                                    _this.getMonitorList(pageNo);
                                    $("#cutdownmoneyMachineBtn").popover("show");
                                    setTimeout(function(){
                                        $("#cutdownmoneyMachineBtn").popover("hide");
                                        $("#cutdownmoneyMachineModal").modal("hide");
                                    }, 300);
                                    

                                }
                                else
                                {
                                    $("#cutdownmoneyMachineModal").modal("hide");
                                    if( code == 2 )
                                    {
                                    
                                        $("#myModalLabel").html("你还未登录");
                                        $("#loginoutModal").modal();
                                        setTimeout(function(){
                                            location.href = "login.html";
                                        }, 800);
                                    }
                                    else
                                    {
                                        $("#myModalLabel").html(desc);
                                        $("#loginoutModal").modal();

                                    }
                                }
                            

                        }


                    });







            });

    

        },
        // 设置开启关闭
        setOnOff:function(mId){
              var _this = this;
                $.ajax({
                    type:"get",
                    url:"/sys?fid=313&siteid="+mId,
                    dataType:"json",
                    success:function( info ){
                        var code = info.code;
                        var desc = info.desc;
                        if( code == 1 )
                        {
                            var data = info.data;
                            var strArr = [];
                            data.forEach(function( item, key ){
                                var desc = item.desc;
                                var state = item.state;//1开启  0 关闭
                                var type = item.type;
                                var ischekced = state==1?"checked":'';
                                var html = '<label    for="type'+key+'"><input type="checkbox"  name="gtype"  '+ischekced+'   id="type'+key+'"  value="'+type+'">'+desc+'</label>';
                                strArr.push(html);
                            });
                            $("#settype").html(strArr.join(""));
                            _this.oprateSel(mId);

                        }
                        else
                        {
                            if( code == 2 )
                            {
                            
                                $("#myModalLabel").html("你还未登录");
                                $("#loginoutModal").modal();
                                setTimeout(function(){
                                    location.href = "login.html";
                                }, 800);
                            }
                            else
                            {
                                $("#myModalLabel").html(desc);
                                $("#loginoutModal").modal();

                            }

                        }

                    }

                });
   
        },
        // 
        oprateSel:function(machineid){
            var  _this = this;
            $("#settype label").unbind("click").click(function(){
                var isSelType = $(this).find("input").prop("checked");
                _this.gametype = $(this).find("input").val();
                if( isSelType )//开启
                {
                    _this.doOnOffMatchine(machineid,1);
                }
                else  //关闭
                {
                    _this.doOnOffMatchine(machineid,0);
                }


            });
           

        },
        // 开启关闭机器
        doOnOffMatchine:function( machineid,onoff ){
            var  gametype =  this.gametype;
            $.ajax({
                type:"get",
                url:"/sys?fid=314&code="+onoff+"&siteid="+machineid+"&type="+gametype,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var tiptext = onoff==1?"开启成功!":"关闭成功!";
                        $("#myModalLabel").html(tiptext);
                        $("#loginoutModal").modal();

                    } 
                    else
                    {
                        if( code == 2 )
                        {
                        
                            $("#myModalLabel").html("你还未登录");
                            $("#loginoutModal").modal();
                            setTimeout(function(){
                                location.href = "login.html";
                            }, 800);
                        }
                        else
                        {
                            $("#myModalLabel").html(desc);
                            $("#loginoutModal").modal();

                        }

                    }

                }
            });

        }
   
       
    };





  new GetMachineMonitor();

}());